//
//  receiveSystemChat.m
//  tcpoctest
//
//  Created by apple on 13-6-3.
//  Copyright (c) 2013年 gump. All rights reserved.
//

#import "receiveSystemChat.h"

#import "XMMessageInfo.h"
#import "ChatContentInfo.h"
#import "giftMessageInfo.h"
#import "GiftInfoDataInfo.h"

#import "GiftMessageDataSource.h"
#import "GiftInfoDataSource.h"

#import "chatuserdb.h"
#import "messagedb.h"
#import "giftMessageDataBase.h"

#import "taskDataSource.h"

#import "sendMessageReceipt.h"
#import "chatDataSource.h"

#import "AudioPlayManager.h"
#import "MessageDataSource.h"
#import "sendChatRead.h"

@implementation receiveSystemChat

-(void)processreceive
{
    //返回 16char(1),4char(1),发送者的用户名长度char(1),
    //发送者的用户名char(x),礼物ID长度char(1),礼物idchar(x),
    //赠送人名称长度char(1)，赠送人名称char(x),int数量
    
    NSString *strsendermail = [logicsb ReadUnencodeString];
    
    [logicsb ReadIntWithTT:NTT_BIG2SMALL];
    
    
    NSString *strgiftid = [logicsb ReadUnencodeString];
    strsendermail = [logicsb ReadUnencodeString];
    int igiftcount = [logicsb ReadIntWithTT:NTT_BIG2SMALL];
    NSString *strchatid = [logicsb ReadUnencodeString];
    NSString *strchattime = [logicsb ReadUnencodeString];
    
    [self AddChatInfoByMail:strsendermail
                 withGiftID:strgiftid
                withGiftNum:igiftcount
               withSendMail:strsendermail
                 withChatID:strchatid
               withChatTime:strchattime];
    
    [[DataControl shareControl].taskDS CompleteTaskByID:TI_F_GETGIFT
     withIsDirectShow:false];
    
    [[netManager sharedNetManager].sendMsgReceipt SendMegReceipt:strchatid];
    
    [self ShowLog];
    
    [[AudioPlayManager shareControl] playSystem:1002];

}

-(void)AddChatInfoByMail:(NSString *)strmail
              withGiftID:(NSString *)strgiftid
             withGiftNum:(int) icount
            withSendMail:(NSString *)sendmail
              withChatID:(NSString *)strchatid
            withChatTime:(NSString *)strchattime
{
    XMMessageInfo *minfo = [[XMMessageInfo alloc] init];
    minfo.strmail = strmail;
    minfo.messageType = XMMT_CHAT;
    minfo.mbAlreadayArgee = false;
    minfo.mindex = [[DataControl shareControl].messageDS  GetMagArrCount];
    minfo.strmsgid = [[DataControl shareControl].messageDS getMsgID];
    
    XMMessageInfo *mexits = [[MessageManager sharedManager] getMessage:minfo];
    ChatContentInfo *cinfo = [[ChatContentInfo alloc] init];
    
    if (mexits == nil)
    {
        
        NSMutableArray *chatshowarr = [[NSMutableArray alloc] init];
        [[DataControl shareControl].chatDS SetChatDicArr:chatshowarr withKey:minfo.strmail];
        [[DataBaseCtrol shareControl].chatuserdatabase insertChatDic:minfo.strmail withIsGroup:false];
        
        
        cinfo.mail = strmail;
        cinfo.chatType = CT_GIFT;
        cinfo.strChatDate = strchattime;
        cinfo.iSequence = [LogicControl sharedLogicControl].CurSequence;
        cinfo.isSelf = false;
        cinfo.giftid = strgiftid;
        cinfo.igiftNum = icount;
        cinfo.strsendmail = sendmail;
        cinfo.strid = strchatid;
        cinfo.ichatReadState = CRS_UNREAD;
        
        [chatshowarr addObject:cinfo];
        
        
        minfo.badgeNum++;
        minfo.strtime = cinfo.strChatDate;
        [[MessageManager sharedManager] AddMessage:minfo];
        
        [[DataControl shareControl].chatDS addChatInfo:cinfo withID:strchatid];
        
        [[DataBaseCtrol shareControl].chatuserdatabase insertChatInfo:cinfo];
        [[DataControl shareControl].UICommendDS addUICommand:UC_RECIVECHECK];
    }
    else
    {
        
        NSMutableArray *chatshowarr = [[DataControl shareControl].chatDS GetArrByKey:strmail];
        if (chatshowarr != nil)
        {
            cinfo.mail = strmail;
            cinfo.chatType = CT_GIFT;
            cinfo.strChatDate = strchattime;
            cinfo.iSequence = [LogicControl sharedLogicControl].CurSequence;
            cinfo.isSelf = false;
            cinfo.giftid = strgiftid;
            cinfo.igiftNum = icount;
            cinfo.strsendmail = sendmail;
            cinfo.strid = strchatid;
            cinfo.ichatReadState = CRS_UNREAD;
            
            [chatshowarr addObject:cinfo];
            
            [[DataControl shareControl].chatDS addChatInfo:cinfo withID:strchatid];
            
            [[DataBaseCtrol shareControl].chatuserdatabase insertChatInfo:cinfo];
        }
        
        mexits.strtime = cinfo.strChatDate;
        
        if ([DataControl shareControl].iGameState == GMS_CHAT &&
            [[DataControl shareControl].currentFI.mail isEqualToString:strmail])
        {
                
                cinfo.ichatReadState = CRS_READ;
                
                [[DataBaseCtrol shareControl].chatuserdatabase UpdateChatState:cinfo];
                
                [[netManager sharedNetManager].sendCR sendChatAlreadyRead:cinfo.strid];
           
            
            [[DataControl shareControl].UICommendDS addUICommand:UC_UPDATACHAT];
        }
        else
        {
            if (mexits.mbAlreadayArgee == true)
            {
                mexits.mbAlreadayArgee = false;
                
                
            }
            
            mexits.badgeNum++;
            
            [[DataControl shareControl].UICommendDS addUICommand:UC_RECIVECHECK];
        }
        
        [[DataBaseCtrol shareControl].messagedatabase updateMessage:mexits];
        
        minfo = nil;
    }
}

@end
